//
//  ExtractSubviewsBootcamp.swift
//  FCSwiftUI
//
//  Created by 冯宣超 on 2023/2/22.
//

import SwiftUI

struct ExtractSubviewsBootcamp: View {
    var body: some View {
        ZStack {
            Color.white.ignoresSafeArea()
            contentLayer
        }
    }
    
    var contentLayer: some View {
        HStack {
            MyItem(count: 50, fruit: .orange)
            MyItem(count: 100, fruit: .apple)
        }
    }
}

struct ExtractSubviewsBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        ExtractSubviewsBootcamp()
    }
}

// 被提取出来的子视图
struct MyItem: View {
    
    enum Fruit {
        case apple
        case orange
    }
    
    let backgroundColor: Color
    let count: Int
    let title: String
    
    init(count: Int, fruit: Fruit) {
        self.count = count
        
        if fruit == .apple {
            title = "Apples"
            backgroundColor = .red
        } else {
            title = "Oranges"
            backgroundColor = .orange
        }
    }
    
    var body: some View {
        VStack(spacing: 12) {
            Text(String(count))
                .font(.largeTitle)
                .foregroundColor(.white)
                .underline()
            
            Text(title)
                .font(.headline)
                .foregroundColor(.white)
        }
        .frame(width: 150, height: 150)
        .background { backgroundColor }
        .cornerRadius(10)
    }
}
